﻿var ManHourReportBiz = function (rHd) {
    var __hd = (typeof (rHd) == "string") ? rHd : "manHourReportBiz";
    var __data = null;
    var __displayer = "";
    var __exportForm = null;
    var __exportFrame = null;
    var __exportDataHandler = null;
    var __buildReport = function () {
        var _str = new Array();
        _str.push('<table class="mhrb_table" cellpadding="0" cellspacing="0">');
        _str.push('<thead class="mhrb_head">');
        _str.push('<tr>');
        _str.push('<th></th>');
        for (var i = 0; i < __data.User.length; i++) {
            _str.push('<th>' + __data.User[i][1] + '</th>');
        }
        _str.push('<td class="mhrb_sum_column">合计</td>');
        _str.push('</tr>');
        _str.push('</thead>');


        _str.push('<tbody class="mhrb_body">');
        var _val = null;
        var _groupName = null;
        var _item = null;
        var _itemName = null;
        var _rowSum = null; //sum pre project by whole users
        var _colSum = new Array();
        var _groupSum = 0;
        var _groupCode = new Array();
        var _rowCode = new Array();
        for (var j = 0; j < __data.User.length; j++) {
            _colSum[j] = 0;
        }
        for (var i = 0; i < __data.Item.length; i++) {
            _rowSum = 0;
            _item = __data.Item[i];
            if (i == 0) {
                _groupName = _item[1].split(',')[1];
            } else if (_groupName != _item[1].split(',')[1]) {
                _groupCode.push('<tr class="mhrb_group">');
                _groupCode.push('<td colspan="' + (__data.User.length + 1) + '">' + _groupName + '</td>');
                _groupCode.push('<td class="mhrb_sum_g">' + __fixFloat(_groupSum) + '</td>');
                _groupCode.push('</tr>');
                _str.push(_groupCode.join('') + _rowCode.join(''));

                _groupName = _item[1].split(',')[1];
                _groupSum = 0;
                _groupCode = new Array();
                _rowCode = new Array();
            }

            _rowCode.push('<tr>');
            _itemName = _item[1].split(',')[0];
            _rowCode.push('<th>' + _itemName + '</th>');
            _val = 0;
            for (var j = 0; j < __data.User.length; j++) {
                _val = __data.Stat[_item[0] + __data.User[j][0]];
                if (_val == null || _val == '' || _val == 0
                    || isNaN(parseFloat(_val))) {
                    _val = 0;
                    _rowCode.push('<td class="mhrb_cell_0">' + _val + '</td>');
                } else {
                    _val = parseFloat(_val);
                    _rowCode.push('<td class="mhrb_cell_1">' + _val + '</td>');
                }
                _rowSum += _val;
                _groupSum += _val;
                _colSum[j] += parseFloat(_val);
            }
            _rowCode.push('<td class="mhrb_sum_r">' + __fixFloat(_rowSum) + '</td>');
            _rowCode.push('</tr>');

            if (i == __data.Item.length - 1) {
                _groupCode.push('<tr class="mhrb_group">');
                _groupCode.push('<td colspan="' + (__data.User.length + 1) + '">' + _groupName + '</td>');
                _groupCode.push('<td class="mhrb_sum_g">' + __fixFloat(_groupSum) + '</td>');
                _groupCode.push('</tr>');
                _str.push(_groupCode.join('') + _rowCode.join(''));
            }
        }
        _str.push('</tbody>');


        _str.push('<tfoot class="mhrb_foot">');
        _str.push('<tr>');
        _str.push('<td class="mhrb_sum_column">合计</td>');
        _rowSum = 0;
        for (var j = 0; j < __data.User.length; j++) {
            _str.push('<td>' + __fixFloat(_colSum[j]) + '</td>');
            _rowSum += parseFloat(_colSum[j]);
        }
        _str.push('<td class="mhrb_sum_r">' + __fixFloat(_rowSum) + '</td>');
        _str.push('</tr>');
        _str.push('</tfoot>');

        _str.push('</table>');
        return _str.join('');
    };
    var __fixFloat = function (v) {
        return (Math.round(v * 100) / 100);
    };
    this.Export = function () {
        var _cssCode = ".report_box{    width:970px;overflow:auto;}TABLE.mhrb_table{    border:1px solid #cccccc;    border-collapse:collapse; }.mhrb_table td,th    {        border:1px solid #cccccc;    border-collapse:collapse;    padding:2px 2px 2px 2px;    }.mhrb_head{    background-color:#336699;color:#ffffff;}.mhrb_head .mhrb_sum_column{    background-color:#ff5500;color:#ffffff;}.mhrb_body{    }.mhrb_body th{    background-color:#888888;color:#ffffff;font-weight:normal;text-align:left;}.mhrb_body td{    background-color:#ffffff;}.mhrb_body .mhrb_cell_0{    color:#aaaaaa;}.mhrb_body .mhrb_cell_1{    color:#000000;}.mhrb_body .mhrb_sum_{    background-color:#eeeeee;color:#ff0000;}.mhrb_body .mhrb_sum_g{    background-color:#ff5500;color:#ffffff;}.mhrb_body .mhrb_group td{    background-color:#555555;font-weight:bold;color:#ffffff;}.mhrb_foot{    }.mhrb_foot td{    background-color:#eeeeee;color:#ff0000;}.mhrb_foot .mhrb_sum_column{    background-color:#ff5500;color:#ffffff;}.mhrb_foot .mhrb_sum_{    background-color:#ff5500;color:#ffffff;}";
        var _html = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">' + _cssCode + '</style></head><body>' + __displayer.innerHTML + '</body></html>';
        if (__exportDataHandler != null) {
            __exportDataHandler.value = _html;
            __exportForm.submit();
        }
    };
    this.load = function (v) {
        __data = v;
    };
    this.deploy = function (rBoxId) {
        if (__data == null) { return; }
        __displayer = $("#" + rBoxId);
        __displayer.innerHTML = __buildReport();
        __exportForm = document.createElement("form");
        __exportForm.setAttribute("id", "frm_" + __hd);
        __exportForm.setAttribute("name", "frm_" + __hd);
        __exportForm.setAttribute("target", "ifm_" + __hd);
        __exportForm.setAttribute("method", "post");
        __exportForm.setAttribute("action", "Ws/_Export.aspx?pn=hid_" + __hd + "&type=html&fn=ManhourExport.html");

        __exportDataHandler = document.createElement("input");
        __exportDataHandler.setAttribute("type", "hidden");
        __exportDataHandler.setAttribute("id", "hid_" + __hd);
        __exportDataHandler.setAttribute("name", "hid_" + __hd);
        __exportForm.appendChild(__exportDataHandler);

        __exportFrame = document.createElement("iframe");
        __exportFrame.style.width = "0px";
        __exportFrame.style.height = "0px";
        __exportFrame.setAttribute("id", "ifm_" + __hd);
        __exportFrame.setAttribute("name", "ifm_" + __hd);
        __exportForm.appendChild(__exportFrame);

        $("body").appendChild(__exportForm);
    };
};